#include "stdafx.h"
#include "CreateAction.h"
#include "defines.h"
#include "DataFlow.h"
#include "TextUnit.h"
#include "RectUnit.h"
#include "DiamondUnit.h"
#include "ArrowUnit.h"

CreateAction::CreateAction(int tempOrder, CPoint p1,CPoint p2)
: preUnit(NULL)
{
	order = tempOrder;
	beginPt = p1;
	endPt = p2;
	id = 0;
}

void CreateAction::redo()
{
	if( 0 == id )
	{
		CUnit* unit;
		if (order==RECT_UNIT )
		{ 
			unit=new CRectUnit();
			unit->ID = id;
			unit->m_BeginPoint = beginPt;
			unit->m_EndPoint = endPt;
			theApp.m_UnitManager->AddShape(unit);
			preUnit = unit;
		}
		if( order == DIAMOND_UNIT)
		{
			unit = new CDiamondUnit();
			unit->ID = id;
			unit->m_BeginPoint = beginPt;
			unit->m_EndPoint = endPt;
			theApp.m_UnitManager->AddShape(unit);
			preUnit = unit;
		}
		if (order==ARROW_UNIT)
		{
			unit = new CArrowUnit();		
			unit->ID = id;
			unit->m_BeginPoint = beginPt;
			unit->m_EndPoint = endPt;
			theApp.m_UnitManager->AddArrow(unit);
			preUnit = unit;
		}
		if (order==ARROW2_UNIT)
		{
			unit = new CArrowUnit();		
			unit->ID = id;
			unit->m_BeginPoint = beginPt;
			unit->m_EndPoint = endPt;
			((CArrowUnit*)unit)->EnableOptimize();
			theApp.m_UnitManager->AddArrow(unit);
			preUnit = unit;
		}
		if (order==TEXT_UNIT)
		{			
			unit = new CTextUnit();
			unit->ID = id;	
			unit->m_BeginPoint = beginPt;
			unit->m_EndPoint = endPt;
			theApp.m_UnitManager->AddShape(unit);
			preUnit = unit;
		}
		id = unit->ID;
// 		beginPt.x = unit->m_BeginPoint.x;
// 		beginPt.y = unit->m_BeginPoint.y;
// 		endPt.x = unit->m_EndPoint.x;
// 		endPt.y = unit->m_EndPoint.y;
	}
	else 
	{
		if( order == ARROW2_UNIT || order == ARROW_UNIT )
			theApp.m_UnitManager->AddArrow(preUnit);
		else
			theApp.m_UnitManager->AddShape(preUnit);
	}
//////////////////////////////////////////////////////////////////////////
//	findUnit(id)->TrackBroad(beginPt,endPt);
//////////////////////////////////////////////////////////////////////////
}

void CreateAction::undo()
{
	findUnit(id)->DistachTracker();
	theApp.m_UnitManager->Delete(findUnit(id));
//////////////////////////////////////////////////////////////////////////
//	findUnit(id)->TrackBroad(beginPt,endPt);
//////////////////////////////////////////////////////////////////////////
}

